import sys
import time

import pyautogui
strs =sys.argv[0].split("/")
if len(strs)==1:
    strs =strs[0].split("\\")
fileName = strs[len(strs)-1]
src =sys.argv[0].replace(fileName,"img/")
def click(img, sim=0.8, flag=0, timeout=10):
    global  src
    timeout1 = timeout
    img = src + img + ".png"
    print(img)
    pos = pyautogui.locateCenterOnScreen(img, confidence=sim)
    x = 0
    while (pos == None):

        pos = pyautogui.locateCenterOnScreen(img, confidence=sim)
        timeout -= 1
        print(timeout)
        time.sleep(0.1)
        if timeout == timeout1 / 2:
            pyautogui.click()
        if timeout < 0:
            pyautogui.moveTo(1000, 500)
            pyautogui.click()
            return
    x = pos.x
    if flag != 0:
        x += flag
    pyautogui.click(x, pos.y)


def find(img, sim=0.7):
    img = src + img + ".png"
    pos = pyautogui.locateCenterOnScreen(img, confidence=sim)
    while (pos == None):
        pos = pyautogui.locateCenterOnScreen(img, confidence=sim)
    return pos
